package com.est.dase.sms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
/**
*
* @author Fernando Geraldes, Jaime Santos, Jorge Silva
*
*/
public class SmsReceiver extends BroadcastReceiver
{

	public SmsReceiver() {
		super();
	}

	
	@Override
	public void onReceive(Context context, Intent intent) 
	{
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
     
        SmsMessage[] msgs = null;
        String str = "";

        if (bundle != null)
        {
            //---retrieve the SMS message received---
        	String messageBody = "";
        	String messageFrom = "";
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                str += "SMS from " + msgs[i].getOriginatingAddress();
                messageFrom = ""+msgs[i].getOriginatingAddress();
                //var.setNumber(msgs[i].getOriginatingAddress());
                str += " : ";
                str += msgs[i].getMessageBody().toString();
                messageBody =msgs[i].getMessageBody().toString();
                str += "\n";
                
            }
          
			if(SMSReceiverHandler.getInstance().getListener()!=null)
				SMSReceiverHandler.getInstance().received(messageFrom, messageBody,context);

            
            /*
            //---display the new SMS message---
            if(messageBody.equals(Params.way)){
            	//AlertDialog.Builder ad = new AlertDialog.Builder(this);
            	Toast.makeText(context, "Do you want to share your location?", Toast.LENGTH_LONG).show();
            }
            else
            	Toast.makeText(context, str, Toast.LENGTH_LONG).show();
            */
        }                 		
	}
		
}